<?php

namespace App\Providers;

use App\Http\Middleware\ApiAccessLog;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                 ->middleware('api')
                 ->namespace($this->namespace)
                 ->group(base_path('routes/api.php'));

            Route::middleware('web')
                 ->namespace($this->namespace)
                 ->group(base_path('routes/web.php'));

            Route::namespace($this->namespace . '\\Common')
                 ->prefix('common')
                 ->group(base_path('routes/common.php'));

            Route::namespace($this->namespace . '\\CallingCards')
                 ->prefix('callingCards')
                 ->group(base_path('routes/callingCards.php'));

            Route::namespace($this->namespace . '\\GuanyuanSSO')
                 ->prefix('guanyuanSSO')
                 ->group(base_path('routes/guanyuanSSO.php'));

            Route::namespace($this->namespace . '\\OpenShop')
                 ->prefix('openShop')
                 ->group(base_path('routes/openShop.php'));

            Route::namespace($this->namespace . '\\Callback')
                 ->prefix('callback')
                 ->group(base_path('routes/callback.php'));

            Route::namespace($this->namespace . '\\Check')
                 ->prefix('check')
                 ->group(base_path('routes/check.php'));

            Route::namespace($this->namespace . '\\GuanyuanDB')
                 ->prefix('guanyuanDB')
                 ->group(base_path('routes/guanyuanDB.php'));

            Route::namespace($this->namespace . '\\WorkOrder')
                 ->prefix('workOrder')
                 ->group(base_path('routes/workOrder.php'));

            Route::namespace($this->namespace . '\\Fee')
                 ->prefix('fee')
                 ->group(base_path('routes/fee.php'));

            Route::namespace($this->namespace . '\\Cruise')
                 ->prefix('cruise')
                 ->group(base_path('routes/cruise.php'));

            Route::namespace($this->namespace . '\\Internal')
                 ->prefix('internal')
                 ->group(base_path('routes/internal.php'));

            Route::namespace($this->namespace . '\\College')
                 ->prefix('college')
                 ->group(base_path('routes/college.php'));

            Route::namespace($this->namespace . '\\SystemManagement')
                 ->prefix('systemManagement')
                 ->group(base_path('routes/systemManagement.php'));

            Route::namespace($this->namespace . '\\Kms')
                 ->prefix('kms')
                 ->group(base_path('routes/kms.php'));

            Route::namespace($this->namespace . '\\Feishu')
                 ->prefix('feishu')
                 ->group(base_path('routes/feishu.php'));

            Route::namespace($this->namespace . '\\Workflow')
                 ->prefix('workflow')
                 ->group(base_path('routes/workflow.php'));

            Route::namespace($this->namespace . '\\Article')
                 ->prefix('article')
                 ->group(base_path('routes/article.php'));

            Route::namespace($this->namespace . '\\ChatGroup')
                 ->prefix('chatGroup')
                 ->group(base_path('routes/chatGroup.php'));

            Route::namespace($this->namespace . '\\Invoice')
                 ->prefix('invoice')
                 ->group(base_path('routes/invoice.php'));

            Route::namespace($this->namespace . '\\Outside')
                 ->prefix('outside')
                 ->group(base_path('routes/outside.php'));

            Route::namespace($this->namespace . '\\Activity')
                 ->prefix('activity')
                 ->group(base_path('routes/activity.php'));
            Route::namespace($this->namespace . '\\marketing')
                 ->prefix('marketing')
                 ->group(base_path('routes/marketing.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)
                        ->by(optional($request->user())->id ?: $request->ip());
        });
    }
}
